﻿using BLL;
using Common;
using Entity;
using Oracle.DataAccess.Client;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using YXYLCommon;

namespace Web.ashx.QSSFZW
{
    /// <summary>
    /// LCCWDataCheck 的摘要说明
    /// </summary>
    public class LCCWDataCheck : IHttpHandler
    {
        
        public void ProcessRequest(HttpContext context)
        {
            string type = context.Request["type"];
            
            if (type== "checkData")
            {
                string flag = "";
                string year = context.Request["year"];
                string month = context.Request["month"];
                string time = year + "-" + month;
                // time = "'" + time + "'";
                string sqlJH = "select * from YS_ZW_LICHENGCWJKB t where s_gongzuony ='" + time + "' and S_beizhu like '%转帐:%' and t.s_Zhaiyao like '%预收%' order by  t.s_Zhaiyao";
                string sqlCW = "select * from YS_ZW_LICHENGCWJKB t where s_gongzuony ='" + time + "' and  S_beizhu like '%非托:%' and t.s_Zhaiyao like '%预收%' order by  t.s_Zhaiyao";

                DataSet dsJH = DBHelp.SelectData(sqlJH);
                DataSet dsCW = DBHelp.SelectData(sqlCW);
                if(dsJH.Tables[0].Rows.Count<=0)
                {
                    flag = "-1";
                    context.Response.Write(flag);
                    return;
                }
                if (dsCW.Tables[0].Rows.Count <= 0)
                {
                    flag = "-1";
                    context.Response.Write(flag);
                    return;
                }
                if (dsJH.Tables[0].Rows.Count != dsCW.Tables[0].Rows.Count)
                {
                    flag = "-2";  //非托(预收) 转账(预收) 数据数量不一致
                    context.Response.Write(flag);
                    return;
                }
                for (int i=0;i<dsJH.Tables[0].Rows.Count;i++)
                {
                    string numJH = dsJH.Tables[0].Rows[i]["n_Jiefangje"].ToString();
                    string numCW = dsCW.Tables[0].Rows[i]["n_daifangje"].ToString(); 
                    string idJH = dsJH.Tables[0].Rows[i]["s_kehudm"].ToString();
                    string idCW = dsCW.Tables[0].Rows[i]["s_kehudm"].ToString();
                    if(idCW==idJH)  //判断用户号是否相同
                    {
                        if (numJH != numCW)
                        {
                            flag = "0"; //检测数据有不同
                            context.Response.Write(flag);
                            return;
                        }
                        else
                        {
                            flag = "1"; //检测通过
                        }
                    }
                    else
                    {
                        flag = "-3"; //用户号不同
                        context.Response.Write(flag);
                    }
                    
                }
                context.Response.Write(flag);

            }
            else if(type== "ShowJH")
            {
                string year = context.Request["year"];
                string month = context.Request["month"];
                string time = year + "-" + month;
                List<CheckDataJH> list = DBHelp.SelectJH(time);
                JavaScriptSerializer jesr = new JavaScriptSerializer();
                string listjson = jesr.Serialize(list);
                context.Response.Write(listjson);

            }
            else if (type == "ShowCW")
            {
                string year = context.Request["year"];
                string month = context.Request["month"];
                string time = year + "-" + month;
                List<CheckDataCW> list = DBHelp.SelectCW(time);
                JavaScriptSerializer jesr = new JavaScriptSerializer();
                string listjson = jesr.Serialize(list);
                context.Response.Write(listjson);

            }
            else if (type == "ShowJHId")
            {
                string year = context.Request["year"];
                string month = context.Request["month"];
                string time = year + "-" + month;
                List<CheckDataJH> list = DBHelp.SelectJHId(time);
                JavaScriptSerializer jesr = new JavaScriptSerializer();
                string listjson = jesr.Serialize(list);
                context.Response.Write(listjson);
            }
            else if (type == "ShowCWId")
            {
                string year = context.Request["year"];
                string month = context.Request["month"];
                string time = year + "-" + month;
                List<CheckDataCW> list = DBHelp.SelectCWId(time);
                JavaScriptSerializer jesr = new JavaScriptSerializer();
                string listjson = jesr.Serialize(list);
                context.Response.Write(listjson);
            }
            else if (type == "ShowJHNum")
            {
                string year = context.Request["year"];
                string month = context.Request["month"];
                string time = year + "-" + month;
                List<CheckDataJH> list = DBHelp.SelectJHNum(time);
                JavaScriptSerializer jesr = new JavaScriptSerializer();
                string listjson = jesr.Serialize(list);
                context.Response.Write(listjson);
            }
            else if (type == "ShowCWNum")
            {
                string year = context.Request["year"];
                string month = context.Request["month"];
                string time = year + "-" + month;
                List<CheckDataCW> list = DBHelp.SelectCWNum(time);
                JavaScriptSerializer jesr = new JavaScriptSerializer();
                string listjson = jesr.Serialize(list);
                context.Response.Write(listjson);
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}